package org.shzhyun.pay.payment;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.shzhyun.pay.config.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class JsApiPay {
    /** 商户API私钥路径 */
    @Value("${pay.privateKeyPath}")
    public String privateKeyPath;
    //商户api证书私钥文件的私钥串
    @Value("${pay.privateKey}")
    public String privateKey;

    public static JsapiServiceExtension jsapiServiceExtension;

    @PostConstruct
    public void init(){
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(Constant.merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(Constant.merchantSerialNumber)
                        .apiV3Key(Constant.apiV3Key)
                        .build();
        // 构建service
//        this.jsapiService = new JsapiService.Builder().config(config).build();
        //JSAPI 支付的扩展类。
        jsapiServiceExtension = new JsapiServiceExtension.Builder().config(config).build();
    }
    public PrepayWithRequestPaymentResponse productOrder(JSONObject dto) {
        // 跟之前下单示例一样，填充预下单参数
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
//        amount.setTotal(Objects.isNull(dto.getInteger("amount"))?1880:dto.getInteger("amount"));
//        amount.setTotal(188);
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid("wx13989a42464bf337");
        request.setMchid(Constant.merchantId);
        request.setDescription("VIP");
        request.setNotifyUrl("https://www.xndpy.com/api/pay/notifyJsApi");
        request.setOutTradeNo("order_vip_"+System.currentTimeMillis());
        Payer payer = new Payer();
        payer.setOpenid(dto.getString("openid"));
        request.setPayer(payer);

        PrepayWithRequestPaymentResponse reponse = null;
        try {
            log.info("===>productOrder=>request:{}",request);
            reponse = jsapiServiceExtension.prepayWithRequestPayment(request);
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("===>productOrder=>reponse:{}",reponse);
        return reponse;
    }

    public String callback(HttpServletRequest request, HttpServletResponse response) {
        return "success";
    }

    /** 关闭订单 */
//    public static void closeOrder() {
//
//        CloseOrderRequest request = new CloseOrderRequest();
//        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
//        // 调用接口
//        service.closeOrder(request);
//    }
    /** JSAPI支付下单，并返回JSAPI调起支付数据 */
//    public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
//        PrepayRequest request = new PrepayRequest();
//        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
//        // 调用接口
//        return service.prepayWithRequestPayment(request);
//    }
    /** 微信支付订单号查询订单 */
//    public static Transaction queryOrderById() {
//
//        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
//        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
//        // 调用接口
//        return service.queryOrderById(request);
//    }
    /** 商户订单号查询订单 */
//    public static Transaction queryOrderByOutTradeNo() {
//
//        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
//        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
//        // 调用接口
//        return service.queryOrderByOutTradeNo(request);
//    }

//    public String productOrder() {
//
//        // request.setXxx(val)设置所需参数，具体参数可见Request定义
//        PrepayRequest request = new PrepayRequest();
//        Amount amount = new Amount();
//        amount.setTotal(1);
//        request.setAmount(amount);
//        request.setAppid("wxa393b7f45e90fb4a");
//        request.setMchid("1659730187");
//        request.setDescription("测试商品标题");
//        request.setNotifyUrl("http://www.xndpy.com/api/jsapipay/callback");
//        request.setOutTradeNo("out_trade_no_001");
//        // 调用下单方法，得到应答
//        PrepayResponse response = this.jsapiService.prepay(request);
//        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
//        log.info(response.getPrepayId());
//        return response.getPrepayId();
//    }
}
